//微信聊天js
$(function(){

	var urlFull=window.location.href;
	var urlArr=urlFull.split("/");
	proName=urlArr[3];
	proName="/"+proName;
	
	//5秒刷新一下聊天窗口
	setInterval("getchat()",5000);
	
	//构建联系人
	queryLinkman();
	
	
	
});	

//头像闪烁
var twinkleHeaderTimer;
function twinkleSysHeader(isTwinkle)
{
	var twinkleOn=true;
	
	if(isTwinkle)
	{
		twinkleHeaderTimer=setInterval(function(){
			if(twinkleOn)
			{
				$("#imgSysHeader").attr({"width":"25px","height":"25px"});
				twinkleOn=false;
			}
			else
			{
				$("#imgSysHeader").attr({"width":"35px","height":"35px"});
				twinkleOn=true;
			}
		},500);
	}
	else
	{
		clearInterval(twinkleHeaderTimer);
		$("#imgSysHeader").attr({"width":"35px","height":"35px"});
	}
	 
}

//获取聊天记录
function getchat()
{
	$.ajax({
		type: "get",
		url: proName+"/weixin/chat/doctor/chatlist",
		success: function(data){
			var chatArr=data;
			if(null!=chatArr&&chatArr.length>0)
			{
				for(var n=0;n<chatArr.length;n++)
				{
					var chatObj=chatArr[n];
					
					var wxOpenId=chatObj.openId;
					var wxNickname=$("#headimg"+wxOpenId).attr("alt");
					
					//新注册的联系人过来了，重新刷新联系人列表
					if(null==wxNickname||""==wxNickname||undefined==wxNickname)
					{
						queryLinkman(wxOpenId,chatObj.content);
					}
					else
					{
						appendChat(wxOpenId,wxNickname,chatObj.content);
					}
				}
				
				//闪烁头像
				twinkleSysHeader(true);
			}
		}
	});
}

/**
 * 添加聊天记录到本地聊天窗口
 */
function appendChat(wxOpenId,wxNickname,chatContent)
{
	var isExist= $("#tabDialogs").tabs("existsById","tabDia"+wxOpenId);
	if(!isExist)
	{
		createDialogWin(wxOpenId,wxNickname);
	}
	
	//修改头像颜色
	$("#boxLinkman"+wxOpenId).css("background-color","#FFE4B5");
	
	var headimg=$("#headimg"+wxOpenId).attr("src");
	var chatStr="";
	chatStr+="<div style='width: 80%;margin-left: 5px;float: left;' align='left'>";
	chatStr+="<div style='float:left;'><img src='"+headimg+"' height='30' width='30'></div>";
	chatStr+="<div style='background-color: #d0d0d0;font-size: 15px;margin-left: 5px;word-break:break-all;width:80%;float:left;'>"+chatContent+"</div>";
	chatStr+="</div>";
	$("#diaCatView"+wxOpenId).append(chatStr);
}

/*
 * 向微信发送消息
 * @param openId 微信OpenId
 */
function addChat(openId)
{
	var wxcontent=$("#txtChatContent"+openId).val();
	var myheadimg=$("#myHeadimg").val();
	$.ajax({
		type: "get",
		url: proName+"/weixin/chat/doctor/chatadd",
		data:{
			openId:openId,
			content:wxcontent
		},
		success: function(data){
			if(data.result==true)
			{
				var chatStr="";
				chatStr+="<div style='width: 80%;margin-right: 5px;float: right;padding: 1px;' align='right'>";
				chatStr+="<div style='float:right;'><img src='"+myheadimg+"' height='30' width='30'></div>";
				chatStr+="<div style='background-color: #96FED1;font-size: 15px;margin-right: 5px;word-break:break-all;width:80%;float:right;'>"+wxcontent+"</div>";
				chatStr+="</div>";
				$("#diaCatView"+openId).append(chatStr);
				$("#txtChatContent"+openId).textbox("setValue","");
			}
			else
			{
				$.messager.alert('警告',data.msg,'warning'); 
			}
		}
	});
}

//打开聊天窗口,并构建内容
function winOpen(openId,nickname)
{
	createDialogWin(openId,nickname);
	winDiaOpen();
	$("#boxLinkman"+openId).css("background-color","#FFFFFF");
}

//打开聊天窗口
function winDiaOpen()
{
	$("#winDialogs").window('open');
	twinkleSysHeader(false);
}

//构建聊天窗口
function createDialogWin(openId,nickname)
{
	var dialogsStr="";
	
	dialogsStr+="<div id='diaCatView"+openId+"' class='easyui-panel' style='height: 70%;' align='center'>";
	dialogsStr+="</div>";
	dialogsStr+="<div style='height: 2%;'></div>";
	dialogsStr+="<div style='height: 15%;'>";
	dialogsStr+="<input id='txtChatContent"+openId+"' class='easyui-textbox' data-options='multiline:true' style='width:100%;height: 100%;'> ";
	dialogsStr+="</div>";
	dialogsStr+="<div style='height: 2%;'></div>";
	dialogsStr+="<div align='center'>";
	dialogsStr+="<a class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-ok'\" href=\"javascript:void(0)\" onclick=\"javascript:addChat('"+openId+"')\" style='width:80px'>发送</a>";
	dialogsStr+="</div>";
	
	var isExist= $("#tabDialogs").tabs("existsById","tabDia"+openId);

	if(!isExist)
	{
		$("#tabDialogs").tabs('add',{
			 id:"tabDia"+openId,
			 title:nickname,
			 closable:true,
			 content:dialogsStr
		});
	}
	else
	{
		$("#tabDialogs").tabs("select",nickname);
	}
	
}

//打开问卷诊疗对话框
function openWinQuestionnaireDiagnosis(url,answerId)
{
	var con = '<iframe id="iframeAnswer48" style="width:100%;height:100%;border:0;" src="'+url+'"></iframe>';  
	$('#questionDiagnosisView').html(con);  
	var winWidth=$(document).width();
	var winHeight=$(document).height();
	$('#winQuestionnaireDiagnosis').window({    
	    width:winWidth,
	    height:winHeight,
	    left:0,
	    top:0,
	    modal:true   
	}); 
	$('#winQuestionnaireDiagnosis').window('open'); 
	$("#questionnaireAnswerId").val(answerId);
	$("#id").val(answerId);
}


//医生提交诊断，检查附件是否上传
function submitDiagnosis()
{
	var questionnaireAnswerId=$("#questionnaireAnswerId").val();
	
	$.ajax({
		type: "post",
		url: proName+"/questionnaire/answer/files",
		data:{
			answerId:questionnaireAnswerId
		},
		success: function(data){
			var answerFiles=data;
			if(null!=answerFiles&&answerFiles.length>0)
			{
				//其他流产方式
				var zdHandleOther=$("#txtZdHandle_other").prop("checked");
				if(true==zdHandleOther)
				{
					var zdHandleOtherCon=$("#txtZdHandle_other_content").textbox("getValue");
					if(null==zdHandleOtherCon||""==zdHandleOtherCon||undefined==zdHandleOtherCon)
					{
						$.messager.alert('警告','请填写其他选项值','error'); 
						return false;
					}
					$("#txtZdHandle_other").val(zdHandleOtherCon);
				}
				
				//提交诊断结果
				submitDiagnosisDo();
			}
			else
			{
				$.messager.alert('提示','需要患者上传检查结果后才能给出诊断！','warning'); 
			}
		}
	});
}

/**
 * 提交诊断结果
 */
function submitDiagnosisDo()
{
	$('#frmQuestionnaireDiagnosis').form('submit',{    
	    url:proName+"/questionnaire/diagnosis",    
	    onSubmit: function(){    
	    	return $(this).form('validate');
	    },    
	    success:function(jsonstr){    
	    	try
	    	{
	    		var data=JSON.parse(jsonstr);   
		    	if(data.result==true)
				{
		    		var myheadimg=$("#myHeadimg").val();
					var openId=data.openId;
					var wxContent=data.content;
					
					var chatStr="";
					chatStr+="<div style='width: 80%;margin-right: 5px;float: right;padding: 1px;' align='right'>";
					chatStr+="<div style='float:right;'><img src='"+myheadimg+"' height='30' width='30'></div>";
					chatStr+="<div style='background-color: #96FED1;font-size: 15px;margin-right: 5px;word-break:break-all;width:80%;float:right;'>"+wxContent+"</div>";
					chatStr+="</div>";
					$("#diaCatView"+openId).append(chatStr);
					
					$('#questionDiagnosisView').html("");  
					$('#winQuestionnaireDiagnosis').window('close'); 
					
					$('#gridUserDetailNoZD').datagrid('load');
				}
		    	else
		    	{
		    		$.messager.alert('提示',data.msg,'info');
		    	}
	    	}catch(e)
	    	{
	    		var msgAle="您的浏览器无法解析JSON，请<a href='"+proName+"/images/plugin/IE自动解析JSON.zip' style='color: blue;'>下载插件</a>！";
	    		$.messager.alert('提示',msgAle,'info');
	    	}
	    }    
	});   
}

/**
 * 获取联系人
 */
function  queryLinkman(openId,chatContent)
{
	$.ajax({
		type: "get",
		url: "/pac/user/linkman/list",
		data:{
			openId:openId
		},
		success: function(data){
			var linkmans=data;
			if(null!=linkmans&&linkmans.length>0)
			{
				for(var n=0;n<linkmans.length>0;n++)
				{
					var linkman=linkmans[n];
					var imgHeaderUrl=linkman.headImg;
					if(null==imgHeaderUrl||""==imgHeaderUrl||undefined==imgHeaderUrl)
					{
						imgHeaderUrl="/pac/images/header/default_header.png";
					}
					
					var strLinkman="<div id='boxLinkman"+linkman.openId+"' style='line-height: 30px;height:30px;width:95%;vertical-align: middle;margin-left: 3px;margin-top:3px;'>";
					strLinkman+="<div style='cursor:pointer;' onclick='winOpen(\""+linkman.openId+"\",\""+linkman.nickname+"\")'>";
					strLinkman+="<img id='headimg"+linkman.openId+"' alt='"+linkman.nickname+"' src='"+imgHeaderUrl+"' height='30' width='30'>";
					strLinkman+="<span style='padding-left: 5px;font-size: 15px;'>";
					strLinkman+=linkman.nickname;
					strLinkman+="</span>";
					strLinkman+="</div>";
					strLinkman+="</div>";
					$("#div_linkmanList").append(strLinkman);
					
					if((null!=openId)&&(""!=openId)&&(undefined!=openId))
					{
						appendChat(openId,linkman.nickname,chatContent)
					}
				}
			}
			else if(null==openId||""==openId||undefined==openId)
			{
				$("#div_linkmanList").append("暂无联系人");
			}
		}
	});
}


//修改病史
function editAnswer()
{
	var ifr = document.getElementById('iframeAnswer48');
	var iframeAnswer = ifr.window || ifr.contentWindow;
	iframeAnswer.editAnswer();
	$("#div_btn_diagnosis").hide();
	$("#div_btn_edit_answer").hide();
	$("#div_btn_edit_answer_do").css("display","inline-block");
}

//病史修改提交
function editAnswerDo()
{
	var ifr = document.getElementById('iframeAnswer48');
	var iframeAnswer = ifr.window || ifr.contentWindow;
	iframeAnswer.editAnswerDo();
	$("#div_btn_diagnosis").css("display","inline-block");
	$("#div_btn_edit_answer").css("display","inline-block");
	$("#div_btn_edit_answer_do").hide();
}

//显示、隐藏工作台【未填写临床观察表患者】列表
function userObserveNoView()
{
	var observeHidden = $("#divGridUserObserveNo").is(":hidden");
	
	if(observeHidden)
	{
		$("#divGridUserObserveNo").show();
		$.parser.parse('#divGridUserObserveNo');
	}
	else
	{
		$("#divGridUserObserveNo").hide();
	}
}
